package com.wu.ssm.controller;


import com.alibaba.fastjson.JSONObject;
import com.wu.ssm.spring.Tool;
import com.wu.ssm.spring.annotion.MyController;
import com.wu.ssm.spring.annotion.MyQualifier;
import com.wu.ssm.spring.annotion.MyRequestMapping;
import com.wu.ssm.entity.Custmoer;
import com.wu.ssm.service.impl.CustmoerServiceImpl;
import com.wu.ssm.service.impl.UserServiceImpl;
import com.wu.ssm.spring.mvc.DataContext;
import com.wu.ssm.spring.mvc.annotion.Request;
import com.wu.ssm.spring.mvc.bean.Multipart;
import com.wu.ssm.spring.mvc.bean.Multiparts;
import com.wu.ssm.spring.mvc.bean.Params;
import com.wu.ssm.spring.utils.JsonUtil;
import com.wu.ssm.spring.utils.UploadHelper;
import com.wu.ssm.spring.utils.WebUtil;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by wzf on 2018/11/4/004.
 */

@MyController(value = "ssm")
@MyRequestMapping(value = "ssm")
public class SpringmvcController {

    @MyQualifier(value = "userService")
    private UserServiceImpl userService;

    @MyQualifier(value = "custmoerService")
    private CustmoerServiceImpl custmoerService;

    @Override
    public String toString() {
        return "重复toString";
    }

    @MyRequestMapping(value = "insert1",method = RequestMethod.GET)
    public String insert1(/*HttpServletRequest request, HttpServletResponse response, String param*/) {
       // userService.insert();
        System.out.println("insert1方法被调用了");

        return null;
    }

    @MyRequestMapping(value = "insert2",method = RequestMethod.POST)
    public String insert2(HttpServletRequest request, HttpServletResponse response, String param) {
        userService.insert();
        System.out.println("带自动注入的insert2方法被调用了");
        return null;
    }

    @Request.Get("/ssm/thransation/{id}")
    public String thransation(long id) {
        System.out.println(id);
        //custmoerService.createCustmoer();
        List<Custmoer> custmoerList = custmoerService.getCustmoerList();
        return "2";
    }

    @Request.Post("/ssm/upload")
    public String upload(Params params, Multiparts multiparts) {


        Map<String, Object> fieldMap = params.getFieldMap();
        Multipart multipart = multiparts.getOne();
        if (multipart != null) {
            fieldMap.put("picture", multipart.getFileName());
        }
        if (multipart != null) {
            UploadHelper.uploadFile(Tool.getBasePath(), multipart);
        }

        return multipart.getFileName();
    }

    @Request.Get("/ssm/download/{filePath}")
    public void download(String filePath) {
        String suffix = ".png";
        WebUtil.downloadFile(DataContext.getResponse(),Tool.getBasePath() + filePath + suffix);
    }


    @Request.Get("/ssm/cachePut")
    public String cachePut() {
        List<Custmoer> custmoerList = custmoerService.getCustmoerList();
        System.out.println(JSONObject.toJSONString(custmoerList));
        return JSONObject.toJSONString(custmoerList);
    }

    @Request.Get("/ssm/cacheClear")
    public String CacheClear() {
        List<Custmoer> custmoerList = custmoerService.CacheClear();
        return JsonUtil.toJson(custmoerList);
    }
}
